"""
sku_id 商品sku id
count 数量
selected 是否勾选
"""

from rest_framework import serializers
from goods.models import SKU


class CartSerializer(serializers.Serializer):
    sku_id = serializers.IntegerField(min_value=1)
    count = serializers.IntegerField(min_value=1)
    selected = serializers.BooleanField(default=True)

    def validate_sku_id(self, value):
        try:
            SKU.objects.get(pk=value)
        except:
            raise serializers.ValidationError('商品不存在')
        return value


class CartSkuSerializer(serializers.ModelSerializer):
    count = serializers.IntegerField(min_value=1)
    selected = serializers.BooleanField()

    class Meta:
        model = SKU
        fields = ('id', 'name', 'default_image_url', 'price', 'count', 'selected')


class CartSkuDeleteSerializer(serializers.Serializer):
    sku_id = serializers.IntegerField()

    def validated_sku_id(self, value):
        try:
            SKU.objects.get(pk=value)
        except:
            raise serializers.ValidationError("商品不存在")
        return value


class CartSelectAllSerializer(serializers.Serializer):
    selected = serializers.BooleanField()
